import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_aygsh/util/Toast.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_footer.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import '../../http/Api.dart';
import '../../http/HttpUtil.dart';
import '../../util/SignUtil.dart';
import '../../base/Constant.dart';
import './MySign.dart';
import '../../entity/common_entity.dart';
import '../../util/AppSaveInfoUtil.dart';
class PointListPage extends StatefulWidget {
  PointListPage({Key key}) : super(key: key);

  @override
  _PointListPageState createState() => _PointListPageState();
}

class _PointListPageState extends State<PointListPage> {
  int page = 1;
  String token = "";
  EasyRefreshController _controller = EasyRefreshController();
List<CommonEntityDataData> dataList = new List();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
         appBar: new AppBar(
        title: Text('我的积分',
            style: TextStyle(
                fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
                color: Colors.black)),
        backgroundColor: Colors.white,
        brightness: Brightness.light,
        centerTitle: true,
        iconTheme: new IconThemeData(color: Colors.black),
        elevation: 0.5,
        
      ),
          body:  Container(
            child: new EasyRefresh(
              enableControlFinishRefresh: true,
              enableControlFinishLoad: true,
              header: MaterialHeader(),
              footer: MaterialFooter(),
              controller: _controller,
              child: ListView.separated(
                  itemBuilder: _renderRow,
                  separatorBuilder: (BuildContext context, int index) {
                    return Container(
                      color: divideColor,
                      height: 1,
                    );
                  },
                  itemCount: dataList.length),
              onRefresh: () async {
                page = 1;
                loadList();
              },
              onLoad: () async {
                page++;
                loadList();
              },
            )
    ));
  }

   Widget _renderRow(BuildContext context, int index) {
    return new GestureDetector(
      child: new Container(
        color: Colors.white,
        padding: EdgeInsets.all(15.0),
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.start,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            new Row(
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: Text(
                    dataList[index].action_zh,
                    style: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                        color: Colors.black),
                  ),
                ),
                Text(
                  dataList[index].action_symbol + dataList[index].point,
                  style: TextStyle(
                      fontSize:
                          ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                      color: Colors.black87),
                ),
              ],
            ),
            Container(
              padding: EdgeInsets.fromLTRB(
                  0, ScreenUtil.getInstance().setHeight(DP_10), 0, 0),
              child: Text(
                dataList[index].create_time,
                style: TextStyle(
                    fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
                    color: Colors.black54),
              ),
            ),
          ],
        ),
      ),
      onTap: () {},
    );
  }

    @override
  void initState() {
    // TODO: implement initState
    super.initState();
setState(() {});
    getString(TOKEN).then((value) {
      token = value;
      loadList();
    });

  }

  loadList() async {
    Map map = new Map();
    map['token'] = token;
    map['p'] = page;
    map['per'] = 10;
    // map['start'] = time1;
    // map['end'] = time2;
    
   map['sign_type'] = createSignType(Api.URL_POINT_RECORD);
    Response<String> response =
        await HttpUtil().post(Api.URL_POINT_RECORD, data: getSign(map));
    Map userMap = json.decode(response.data);
    if (userMap['code'] != 1) {
      Toast.toast(context,msg: userMap['msg']);
      // Fluttertoast.showToast(msg: userMap['msg']);
      return;
    }
    CommonEntityEntity homeListEntity =
        new CommonEntityEntity.fromJson(userMap);
    if (mounted)
      setState(() {
        if (page == 1) {
          dataList.clear();
          _controller.finishRefresh();
          _controller.resetLoadState();
          dataList.addAll(homeListEntity.data.datas);
        } else {
          dataList.addAll(homeListEntity.data.datas);
          _controller.finishLoad(noMore: homeListEntity.data.datas.length == 0);
        }
      });
  }
}